package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import data.DataConn;

public class showSearch {
	public static int allsize;
	public static int allpage;
	public ArrayList searchlist(String q, int nowpage, int pagesize) {
		ArrayList list = new ArrayList();
		String sql="SELECT * FROM content where title like '%"+q+"%' or content like '%"+q+"%' order by id desc";
		try {

			DataConn dataconn = new DataConn();
			Connection conn = dataconn.getConn("root", "root", "test");
			Statement statement = dataconn.getStatement(conn);
			ResultSet rs = dataconn.getResultSet(statement, sql);
			rs.last();
			allsize = rs.getRow();			
			if(nowpage<1) nowpage=1;			
			allpage = (allsize+pagesize-1)/pagesize;
			if(nowpage>allpage)
				nowpage = allpage;
			if(allpage>0){
				rs.absolute((nowpage-1)*pagesize+1);
				int i=0;
				while(i<pagesize&&!rs.isAfterLast()) {
					SearchBean solist = new SearchBean();
					solist.setId(rs.getInt("id"));
					solist.setTitle(rs.getString("title"));
					solist.setHitsnum(rs.getInt("HitsNum"));
					solist.setUser(rs.getString("puser"));
					solist.setPosttime(dataconn.toStrChnYmdHms(rs.getTimestamp("posttime")));
					list.add(solist);

					rs.next();
					i++;	
				}
			}

			conn.close();
			statement.close();
			rs.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return list;
	}

}
